<?php
require_once('MySqlConnection.php');
require_once('Profession.php');

class ProfessionDAL{

	private $_connection;
	
	//SINGLETON PATTERN
    private static $_instance = null;
	public static function Instance(){
		if (self::$_instance == null) self::$_instance = new ProfessionDAL();
		return self::$_instance;
	}
	//SINGLETON PATTERN END
	
	private function __construct(){
		include_once('./include/db.inc');
		$this->_connection = MySqlConnection::Instance($server,$user,$password,$database);
	}
	
	public function getProfessions(){
		$this->_connection->Open();
		$sqlString = "select * from profession";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'profession');
	}
	
	public function getProfessionById($profId){
		$this->_connection->Open();
		$sqlString = "select * from profession where id = '{$profId}'";
		$resultData = $this->_connection->ExecuteSQL($sqlString, 'O');
		return $this->Mapping($resultData, 'profession');
	}
	
	public function addProfession($profession){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("INSERT INTO profession(name) VALUES(?)")) {
			$stmt->bind_param("s",$profession['name']);
			$success = $stmt->execute();
			if(!$success){
				echo "Inserting profession failed!";
			}
		}
		else{
			
			echo "Failed preparing insert profession statement!";
		}
		$mysqli -> close();
	}
	
	public function removeProfession($profId){
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("DELETE FROM profession WHERE id = ?")) {
			$stmt->bind_param("i", $profId);
			$success = $stmt->execute();
			if(!$success){
				echo "Removing profession failed!";
			}
		}
		else{
			echo "Failed preparing delete profession statement!";
		}
		$mysqli -> close();
	}
	
	public function updateProfession($profession) {
		include('./include/db.inc');
		$mysqli = new mysqli($server, $user, $password, $database);
		if(mysqli_connect_errno()) {
		  echo "Connection Failed: " . mysqli_connect_errno();
		  exit();
		}
		if($stmt = $mysqli->prepare("UPDATE profession SET name = ? WHERE id = ?")) {
			$stmt->bind_param("si", $profession['name'], $profession['id']);
			$success = $stmt->execute();
			if(!$success){
				echo "Updating profession failed!";
			}
		}
		else{
			echo "Failed preparing update profession statement!";
		}
		$mysqli -> close();
	}
	
	//MAPPING	
	private function Mapping($queryData,$resultType){
		foreach($queryData as $data){
			$resultArray[] = $this->Map($data,$resultType);
		}
		return $resultArray;
	}
	
	private function Map($data,$resultType){
		$object = new Profession($data->id,$data->name);
		return $object;
	}
	//MAPPING END
	
}

